﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BranchOfSansar.Sansar.Register;



namespace BranchOfSansar.Register.Authentication
{
    public partial class frmParameterEdit : Form
    {
        private string Group { get; set; }
        public frmParameterEdit(string group)
        {
            InitializeComponent();
            txtCode.Enabled = true;
        }

        public frmParameterEdit(string group, Parameter para):this(group)
        {
            Group = group;
            txtCode.Text = para.ParaCode;
            txtDesc.Text = para.Name;
            txtStatus.Checked = para.Status;
            txtCode.Enabled = false;
 
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            

            if (string.IsNullOrEmpty(txtCode.Text) || txtCode.Text.Length != 3)
            {
                Globals.Warr("Код хоосон байж болохгүй. 3 оронтой байх ёстой");
                txtCode.Focus();
                return;
            }

            if (string.IsNullOrEmpty(txtDesc.Text))
            {
                Globals.Warr("Утга хоосон байж болохгүй.");
                txtDesc.Focus();
                return;
            }

            Parameter obj = new Parameter() { 
                ParaCode=txtCode.Text,
                Status=txtStatus.Checked,
                Name=txtDesc.Text,
                GroupCode=Group
            };


            using (RegisterClient rq = new RegisterClient(Globals.Token))
            {
                Result res = null;
                if (txtCode.Enabled)
                    res = rq.AddParameter(obj);
                else
                    res = rq.EditParameter(obj);

                if (res.Code != 0)
                    Globals.Err(res.Message);
                else 
                {
                    Globals.Info(res.Message);
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                }                

            }
        }
    }
}
